import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';

class CustomListPage extends StatefulWidget {

  @override
  _CustomListState createState() => _CustomListState();

}

class _CustomListState extends State<CustomListPage> {

  List<String> contents = [];
  bool noData = false;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 2), (){
      setState(() {
        contents = ['one', 'two', 'three', 'four', 'five'];
        noData = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('带空页面的列表'),
      ),
      body: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(),
              ),
              Expanded(
                flex: 1,
                child: FlatButton(
                  child: Text('有数据'),
                  onPressed: (){
                    setState(() {
                      contents = ['one', 'two', 'three', 'four', 'five'];
                      noData = false;
                    });
                  },
                  color: Colors.blue,
                  textColor: Colors.white,
                )
              ),
              Expanded(
                flex: 1,
                child: Container(),
              ),
              Expanded(
                flex: 1,
                child: FlatButton(
                  child: Text('无数据'),
                  onPressed: (){
                    setState(() {
                      contents = [];
                      noData = true;
                    });
                  },
                  color: Colors.blue,
                  textColor: Colors.white,
                )
              ),
              Expanded(
                flex: 1,
                child: Container(),
              ),
            ],
          ),
          Expanded(
            flex: 1,
            child: customListView(
                scrollView: ListView.separated(itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: EdgeInsets.only(left: 30, top: 20, bottom: 20),
                    child: Text(contents[index]),
                  );
                }, itemCount: contents.length,
                  separatorBuilder: (BuildContext context, int index) {
                    return Divider(color: Colors.grey, height: 2);
                  },
                ),
                noData: noData
            ),
          )
        ],
      ),
    );
  }

}

Widget customListView({@required ScrollView scrollView, bool noData = false}) {
  return noData ? noDataWidget(noSpace: false) : scrollView;
}

Widget noDataWidget(
    {String hint = '暂无数据',
      num size,
      num fontSize,
      bool noSpace = true}) {
  return Column(
    children: <Widget>[
      noSpace
          ? Padding(
        padding: EdgeInsets.only(top: ScreenUtil().setWidth(200)),
      )
          : Expanded(
        flex: 1,
        child: Container(),
      ),
      Image.asset('assets/images/no_data.png',
          width: size ?? ScreenUtil().setWidth(400),
          height: size ?? ScreenUtil().setWidth(400)),
      Padding(
        padding: EdgeInsets.only(top: ScreenUtil().setWidth(50)),
      ),
      Text(hint,
          style: TextStyle(
              fontSize: fontSize ?? ScreenUtil().setSp(40),
              color: Colors.grey)),
      noSpace
          ? Padding(
        padding: EdgeInsets.only(top: ScreenUtil().setWidth(200)),
      )
          : Expanded(
        flex: 2,
        child: Container(),
      ),
    ],
  );
}